package com.training.admin.web.controller.system;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.training.admin.entity.trainee.Trainee;
import com.training.admin.service.TraineeService;

/**
 * 
 * @author Rockett
 * 
 */
@Controller
@RequestMapping("/trainees")
@MenuSetting("menu-system-management")
public class TraineeController {

	private Logger logger = LoggerFactory.getLogger(TraineeController.class);

	@Autowired
	private TraineeService traineeService;

	/**
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@MenuSetting("submenu-trainee-management")
	public ModelAndView list(Page page, HttpServletRequest request) {

		ModelAndView modelAndView = new ModelAndView("trainee/trainee-list");

		int totals = traineeService.count(Trainee.class);

		if (totals > 0) {

			List<Trainee> trainees = traineeService.listByPage(Trainee.class, page.with(totals));
			modelAndView.addObject("trainees", trainees);
			modelAndView.addObject("page", page);
		}
		modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));

		return modelAndView;
	}

	/**
	 * 学员管理同步
	 * 
	 * @return
	 */
	@RequestMapping(value = "/sync", method = RequestMethod.GET)
	@MenuSetting("submenu-trainee-management")
	public ModelAndView sync() {

		ModelAndView modelAndView = new ModelAndView("trainee/trainee-sync");

		return modelAndView;
	}

	/**
	 * @param id
	 * @param principal
	 * @param districtId
	 * @return
	 */
	@RequestMapping(value = "/sync", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public @ResponseBody HttpResult onSync() {

		HttpResult httpResult = new HttpResult();
		try {
			traineeService.saveAllIdmUser();

			httpResult.success().message("同步数据成功!");
		} catch (Exception e) {
			httpResult.fail("500").message("同步数据失败!");
			logger.error("#onSync() - sync fail, exception is [{}]", e);
		}

		return httpResult;
	}

	/**
	 * @param search
	 * @return
	 */
	@RequestMapping(value = "/select-search", params = "query", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public @ResponseBody HttpResult selectSearch(@RequestParam("query") String q,
	                                             @RequestParam(name = "currentProjectId", required = false) Long currentProjectId) {

		HttpResult httpResult = new HttpResult();

		List<Map<String, String>> maps = traineeService.listIdmUser(q, currentProjectId);
		httpResult.success().data("trainees", maps);

		return httpResult;
	}

}
